package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.sms.service.SmsBizzService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.enums.UserAuthAuditStatusDictEnum;
import com.qqtech.qquan.user.model.UserAuth;
import com.qqtech.qquan.user.model.UserAuthQuery;
import com.qqtech.qquan.user.model.UserAuthVo;
import com.qqtech.qquan.user.model.UserLoginVo;
import com.qqtech.qquan.user.service.UserAuthService;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserLoginService;

@Controller
@RequestMapping(value = "/user/auth")
public class UserAuthController extends BaseAdminController {
	@Resource
	private UserAuthService userAuthService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private UserLoginService userLoginService;

	@Resource
	private SmsBizzService smsBizzService;

	@Resource
	private PushBizzService pushBizzService;

	@Override
	protected BaseService<UserAuth> getBaseService() {
		return userAuthService;
	}

	/**
	 * 列表页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/user/base/userauth-list";
	}

	/**
	 * 获取列表数据
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserAuthQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserAuthVo> page = userAuthService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String detail(Integer userId, Model model) {
		UserAuthVo userAuthVo = (UserAuthVo) userAuthService.getByUserId(userId);
		model.addAttribute("userAuthVo", userAuthVo);
		return "/user/base/userauth-detail";
	}

	/**
	 * 认证通过
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/through", method = RequestMethod.POST)
	@ResponseBody
	public Result through(UserAuthVo userAuth) {
		Result ret = new Result();
		if (userAuth == null || userAuth.getUserId() == null) {
			ret.addErr("参数不能为空");
		}
		if (StringUtil.isBlank(userAuth.getBirthPlace())) {
			ret.addErr("籍贯不能为空");
		}
		if (userAuth.getBirthAt() == null) {
			ret.addErr("出生年日不能为空");
		}
		if (ret.checkForm()) {
			UserLoginVo userLoginVo = userLoginService.getById(userAuth.getUserId());
			userLoginVo.setHasIdentityAuth(YesNoEnum.YES.getCode());
			userLoginService.updateById(userLoginVo);
			userLoginService.clear(userLoginVo.getToken());
			ret = userAuthService.handleThrough(userAuth, getLoginUser().getId());
			if (ret.checkForm()) {
				smsBizzService.realAuth(userLoginVo.getId(), YesNoEnum.YES);
				pushBizzService.realAuth(userLoginVo.getId(), YesNoEnum.YES);
			}
		}
		return ret;
	}

	/**
	 * 不通过
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/notthrough", method = RequestMethod.POST)
	@ResponseBody
	public Result notthrough(Integer userId) {
		Result ret = new Result();
		UserAuth userAuth = null;
		if (userId == null) {
			ret.addErr("参数不能为空");
		} else {
			userAuth = userAuthService.getByUserId(userId);
			if (userAuth == null) {
				ret.addErr("未申请身份认证");
			} else if (userAuth.getAuditStatus().intValue() == UserAuthAuditStatusDictEnum.THROUGH.getCode()) {
				ret.addErr("已认证通过");
			}
		}
		if (ret.checkForm()) {
			UserAuth temp = new UserAuth();
			temp.setId(userAuth.getId());
			temp.setAuditStatus(UserAuthAuditStatusDictEnum.NOT_THROUGH.getCode());
			temp.setAdminUserId(getLoginUser().getId());
			temp.setAuditStatusAt(TimeUtil.now());
			temp.setUpdAt(temp.getAuditStatusAt());
			userAuthService.updateById(temp);
			smsBizzService.realAuth(userAuth.getUserId(), YesNoEnum.NO);
			pushBizzService.realAuth(userAuth.getUserId(), YesNoEnum.NO);
		}
		return ret;
	}

}
